package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = permissionService.findPage(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true,"权限添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限添加失败");
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,"回显成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"回显失败");
        }
    }

    @RequestMapping("update")
    public Result update(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
            return new Result(true,"权限编辑成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限编辑失败");
        }
    }

    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try {
            permissionService.deleteById(id);
            return new Result(true,"权限删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    /**
     * 查询所有的权限信息
     *
     * @return
     */
    @RequestMapping("/permissionAll")
    public Result permissionAll() {
        try {
            //调用service中的查询方法
            List<Permission> list = permissionService.permissionAll();
            //查询成功, 返回查询成功的信息
            return new Result(true, MessageConst.ACTION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败, 返回查询失败的信息
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
